package com.fengqianrun.bootframework.util;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.security.exp.IErrorCode;
import com.aizuda.easy.security.exp.impl.BasicException;
import lombok.SneakyThrows;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;


public class AssertUtil {


    @SneakyThrows
    public static void objIsNull(Object obj, IErrorCode code){
        if(ObjectUtil.isEmpty(obj) || ObjectUtil.isNull(obj)){
            throw new BasicException(code);
        }
    }

    @SneakyThrows
    public static void objIsNotNull(Object obj, IErrorCode code){
        if(ObjectUtil.isNotNull(obj) || ObjectUtil.isNotEmpty(obj)){
            throw new BasicException(code);
        }
    }

    /**
     * 对象是否相等
     * @param obj1
     * @param obj2
     * @param code
     */
    @SneakyThrows
    public static void objEQ(Object obj1, Object obj2,IErrorCode code){
        objIsNull(obj1,code);
        objIsNull(obj2,code);
        if(ObjectUtil.equals(obj1,obj2)){
            throw new BasicException(code);
        }
    }

    /**
     * 处理字符串
     */

    public static class Str{

        @SneakyThrows
        public static void strIsNull(String obj1,IErrorCode code){
            if(StrUtil.isEmpty(obj1)){
                throw new BasicException(code);
            }
        }

        @SneakyThrows
        public static void strEQ(String obj1,String obj2,IErrorCode code){
            strIsNull(obj1,code);
            strIsNull(obj2,code);
            if(!obj1.equals(obj2)){
                throw new BasicException(code);
            }
        }

    }

    /**
     * 处理时间
     */
    public static class Time{
        /**
         * 比较日期 start 是否在 end之前
         * @param start
         * @param end
         * @return
         */
        @SneakyThrows
        public static void eqDate(String start,String end,IErrorCode code){
            LocalDate date1 = LocalDate.parse(start);
            LocalDate date2 = LocalDate.parse(end);
            if(!date1.isBefore(date2)){
                throw new BasicException(code);
            }
        }

        /**
         * 比较时间 start 是否在 end之前
         * @param start
         * @param end
         * @return
         */
        @SneakyThrows
        public static void eqDateTime(String start,String end,IErrorCode code){
            LocalDateTime date1 = LocalDateTime.parse(start);
            LocalDateTime date2 = LocalDateTime.parse(end);
            if(!date1.isBefore(date2)){
                throw new BasicException(code);
            }
        }

        @SneakyThrows
        public static void eqDateTime(Date start, Date end, IErrorCode code){
            LocalDateTime date1 = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            LocalDateTime date2 = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            if(!date1.isBefore(date2)){
                throw new BasicException(code);
            }
        }

        /**
         * 必将两个时间是否相等
         * @param start
         * @param end
         * @param code
         */
        @SneakyThrows
        public static void eqDate(Date start, Date end, IErrorCode code){
            LocalDate date1 = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate date2 = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            if(!date1.isBefore(date2)){
                throw new BasicException(code);
            }
        }





    }

    /**
     * 处理集合
     */
    public static class List{
        @SneakyThrows
        public static void isEmpty(java.util.List list, IErrorCode code){
            objIsNull(list,code);
            if(CollUtil.isEmpty(list)){
                throw new BasicException(code);
            }
        }

        @SneakyThrows
        public static void isEmpty(Object[] obj, IErrorCode code){
            objIsNull(obj,code);
            isEmpty(Arrays.asList(obj),code);
        }

    }

}
